package com.company;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.company.DivisionInAColumn;

/**
 * The class Division provides the user interface, prepares data for calling the main class(DivisionInAColumn) and writes results.
 * @author Dusheyko Dima
 * @version 1.05 05.10.2013
 */
public class Division {

    private static int convertBufferReaderToInt(BufferedReader currentBuffer) throws IOException {
        int currentInt      = 0;
        while (true) {
            String currentString = currentBuffer.readLine();
            try {
                currentInt  = Integer.parseInt(currentString);
                break;
            } catch (NumberFormatException e) {
                System.out.print("Error! Value hasn't a number format, Please enter a correct number: ");
            }
        }
        return currentInt;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Please enter a dividend: ");
        int dividend = convertBufferReaderToInt(input);
        System.out.print("Please enter a divisor: ");
        int divisor  = convertBufferReaderToInt(input);

        if (divisor == 0) {
            System.out.println("Error!. You can't divide by 0");
        }
        else {
            System.out.println();
            System.out.println(dividend + "|" + divisor);

            Float divisionForASign = (float) dividend / divisor;
            DivisionInAColumn divisionOfIntegersInAColumnMy
                    = new DivisionInAColumn(divisor, dividend, divisionForASign);

            String result = divisionOfIntegersInAColumnMy.getResult();
            int positionOfPoint = result.indexOf(".");
            System.out.println();
            System.out.format("The quotient = %s", (positionOfPoint == -1 ? result : result.substring(0, positionOfPoint))).println();
            System.out.format("The rest     = %s", (positionOfPoint == -1 ? 0 : result.substring(positionOfPoint + 1))).println();
            System.out.format("The result   = %s", result);
        }
    }
}